Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

227
Visualizações
Jquery Taking action when 2 inputs are "checked"

I have 4 input checkbox elements with the same name 2 / 2. When one changes, I want to change the other. If one is checked, the other must also be checked.

same time; the following "div" should close if both are not "checked" I tried to create an if structure, but without success.

$(document).ready(function() {
    $('.control').change(function() {
        let changedName = $(this).attr('name');
        $("."+changedName).slideToggle();

        let isCheckBox = $("input[name="+changedName+"]");
    
        $(isCheckBox).prop('checked', this.checked); //biri "checked" olduğunda, aynı isme sahip diğer elemanı da "checked" yapıyorum

        let ischecked = $("input[name=changer-one]").is(':checked'); // I tried to check here, I tried getting true and false values
        let ischeckedTwo = $("input[name=changer-two]").is(':checked'); // I tried to check here, I tried getting true and false values


        if (ischecked || ischeckedTwo) { // ".options" will appear when one of them is not "checked"
            $(".options").fadeIn(300);
        } else if (ischecked && ischeckedTwo) { // ".options" will be hidden when both are "checked"
            $(".options").fadeOut(300);
        };

    });
});
.options {
display:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="content-changer">
  <label>
    <input name="changer-one" class="control" type="checkbox" checked />
    <div><span></span></div>
  </label>
</div>
<div class="content-changer">
  <label>
    <input name="changer-one" class="control" type="checkbox" checked />
    <div><span></span></div>
  </label>
</div>
<br><br>
<div class="content-changer">
  <label>
    <input name="changer-two" class="control" type="checkbox" checked />
    <div><span></span></div>
  </label>
</div>
<div class="content-changer">
  <label>
    <input name="changer-two" class="control" type="checkbox" checked />
    <div><span></span></div>
  </label>
</div>

<div class="options">
  Lorem ipsum dolor
</div>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need to negate the first condition, according to the comment in the code.

if(!ischecked || !ischeckedTwo){ ... }

$(document).ready(function() {
  $('.control').change(function() {
    let changedName = $(this).attr('name');
    $("." + changedName).slideToggle();

    let isCheckBox = $("input[name=" + changedName + "]");

    $(isCheckBox).prop('checked', this.checked); //biri "checked" olduğunda, aynı isme sahip diğer elemanı da "checked" yapıyorum

    let ischecked = $("input[name=changer-one]").is(':checked'); // I tried to check here, I tried getting true and false values
    let ischeckedTwo = $("input[name=changer-two]").is(':checked'); // I tried to check here, I tried getting true and false values


    if (!ischecked || !ischeckedTwo) { // ".options" will appear when one of them is not "checked"
      $(".options").fadeIn(300);
    } else if (ischecked && ischeckedTwo) { // ".options" will be hidden when both are "checked"
      $(".options").fadeOut(300);
    };

  });
});
.options {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="content-changer">
  <label>
    <input name="changer-one" class="control" type="checkbox" checked />
    <div><span></span></div>
  </label>
</div>
<div class="content-changer">
  <label>
    <input name="changer-one" class="control" type="checkbox" checked />
    <div><span></span></div>
  </label>
</div>
<br><br>
<div class="content-changer">
  <label>
    <input name="changer-two" class="control" type="checkbox" checked />
    <div><span></span></div>
  </label>
</div>
<div class="content-changer">
  <label>
    <input name="changer-two" class="control" type="checkbox" checked />
    <div><span></span></div>
  </label>
</div>

<div class="options">
  Lorem ipsum dolor
</div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda